import axios from "axios";
import qs from "qs";
import Utils from "./util";
import { ElMessage } from "element-plus";
import { getToken, setToken, removeToken } from "@/utils/auth";
import router from "@/router/index";

/* 请求头类型 */
//formData类型
const FORM_DATA_TYPE = {
  headers: {
    "Content-Type": "application/x-www-form-urlencoded;charset=utf-8",
  },
};
//JSON类型
const JSON_TYPE = {
  headers: {
    "Content-Type": "application/json",
  },
};

//短信验证类型
const text_TYPE = {
  headers: {
    "Content-Type": "application/json",
  },
};

const timeout = 60000;

//创建axios实例
const service = axios.create({
  timeout: timeout,
});

//请求拦截器
service.interceptors.request.use(
  (config) => {
    //请求前将参数转为相对应类型
    const systemToken = getToken();
    if (
      systemToken !== undefined &&
      systemToken !== null &&
      systemToken !== ""
    ) {
      config.headers.token = systemToken;
    }else{
      config.headers.token = getToken();
    }
    if (config.method === "get") {
      if (config.headers["Content-Type"] === "application/json") {
        return config;
      } else {
        config.data = qs.stringify(config.data);
      }
    } else if (config.method === "post") {
      if (config.headers["Content-Type"] === "application/json") {
        return config;
      } else {
        config.data = qs.stringify(config.data);
      }
    }
    return config;
  },
  (error) => {
    console.log("service request", error);
    Promise.reject(error);
  }
);
// 响应拦截器
service.interceptors.response.use(
  (response:any) => {
    if (response && response.status) {
      //这里可以做统一的处理
      if (response.data.code == 400) {
        ElMessage({
          message: "token失效   " + response.data.message,
          type: "error",
        });
        removeToken();
        router.push("/login");
      } 
      return response;
    }
  },
  (error) => {
    switch (error.response.status) {
      // 对得到的状态码的处理，具体的设置视自己的情况而定
      case 401:
        ElMessage({
          message: "token失效   " + error.response.data,
          type: "error",
        });
        break;
      case 404:
        ElMessage({
          message: error.response.data,
          type: "error",
        });
        break;
      case 405:
        ElMessage({
          message: error.response.data,
          type: "error",
        });
        break;
      // case ...
      default:
        ElMessage({
          message: error.response.data,
          type: "error",
        });
        break;
    }

    return Promise.resolve(error.response);
  }
);

export default {
  //post请求 fromData参数类型
  post: function (url:string, params:any, successCallBack:any, errorCallBack:any) {
    service
      .post(url, params, FORM_DATA_TYPE)
      .then(function (response) {
        if (response && response.status.toString() === "200") {
          successCallBack(response);
        } else {
          errorCallBack(response);
        }
      })
      .catch(function (error) {
        errorCallBack(error);
      });
  },
  //psot JSONT
  postJson: function (url:string, params:any, successCallBack:any, errorCallBack:any) {
    service
      .post(url, params, JSON_TYPE)
      .then(function (response) {
        if (response && response.status.toString() === "200") {
          successCallBack(response);
        } else {
          errorCallBack(response);
        }
      })
      .catch(function (error) {
        errorCallBack(error);
      });
  },
  //post Query
  postQuery: function (url:string, params:any, successCallBack:any, errorCallBack:any) {
    url = url + "?" + Utils.urlEncode(params);
    service
      .post(url, {}, text_TYPE)
      .then(function (response) {
        // console.log(response)
        if (response && response.status == 200) {
          successCallBack(response);
        } else {
          errorCallBack(response);
        }
      })
      .catch(function (error) {
        errorCallBack(error);
      });
  },
  deleteJson: function (url:string, params:any, successCallBack:any, errorCallBack:any) {
    service
      .delete(url, params)
      .then(function (response) {
        if (response && response.status.toString() === "200") {
          successCallBack(response);
        } else {
          errorCallBack(response);
        }
      })
      .catch(function (error) {
        errorCallBack(error);
      });
  },
  deleteQuery: function (url:string, params:any, successCallBack:any, errorCallBack:any) {
    url = url + "?" + Utils.urlEncode(params);
    service
      .delete(url, {})
      .then(function (response) {
        // console.log(response)
        if (response && response.status == 200) {
          successCallBack(response);
        } else {
          errorCallBack(response);
        }
      })
      .catch(function (error) {
        errorCallBack(error);
      });
  },
  put: function (url:string, params:any, successCallBack:any, errorCallBack:any) {
    service
      .put(url, params, FORM_DATA_TYPE)
      .then(function (response) {
        if (response && response.status.toString() === "200") {
          successCallBack(response);
        } else {
          errorCallBack(response);
        }
      })
      .catch(function (error) {
        errorCallBack(error);
      });
  },
  putJson: function (url:string, params:any, successCallBack:any, errorCallBack:any) {
    service
      .put(url, params, JSON_TYPE)
      .then(function (response) {
        if (response && response.status.toString() === "200") {
          successCallBack(response);
        } else {
          errorCallBack(response);
        }
      })
      .catch(function (error) {
        errorCallBack(error);
      });
  },
  get: function (url:string, params:any, successCallBack:any, errorCallBack:any) {
    url = url + "?" + Utils.urlEncode(params);
    service
      .get(url, {})
      .then(function (response) {
        // console.log(response)
        if (response && response.status == 200) {
          successCallBack(response);
        } else {
          errorCallBack(response);
        }
      })
      .catch(function (error) {
        errorCallBack(error);
      });
  },
};